Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

double event issue #577

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

double event issue #577

wants to merge 3 commits into from

Conversation

Akhill2020
Copy link
Contributor

@Akhill2020 Akhill2020 commented Sep 30, 2024

Description: There was an issue in which if the event is set for full day and more then one day then it will duplicate the event first day Also, in my testing, I have found that the event is not displayed on months last day so I have updated the event filter code and add some condition that prevent from duplicate event on the day.
After: https://www.screenpresso.com/=pPD51FQi7X9e
clickup: https://app.clickup.com/t/86cwkpk16
issue: https://wordpress.org/support/topic/all-day-events-are-duplicated-on-the-calendar/
https://wordpress.org/support/topic/deprecated-warning-25/
#575

@Akhill2020 Akhill2020 linked an issue Sep 30, 2024 that may be closed by this pull request

// Put resulting events in an associative array, with day of the month as key for easy retrieval in calendar days loop.
$current_month = Carbon::createFromTimestamp($this->start, $calendar->timezone)->month;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the changes starting from this line till 382. Can you please explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$current_month = Carbon::createFromTimestamp($this->start, $calendar->timezone)->month; $current_year = Carbon::createFromTimestamp($this->start, $calendar->timezone)->year;
in first two lines, get the current month and current year,

`$filtered = array_filter($events, function ($events_in_day) {
foreach ($events_in_day as $event) {

This foreach block filters the events to include only those that occur within the current calendar view's start and end timestamps.
It checks if the event's start time is within the range or if the event's end time falls within the range.
The 86400 seconds (24 hours) is added to the end timestamp to include events that end at the very end of the day.`

$day_events = [];
foreach ($filtered as $timestamp => $events_in_day) {
from this line Initializes an empty array to hold events for each day. It iterates over the filtered events, creating Carbon instances for the start and end times of each event, adjusted for the calendar's timezone.

for ($day = $event_start->copy(); $day->lte($event_end); $day->addDay()) {
This loop iterates through each day from the event's start to its end.
It checks if the day falls within the current month and year.
It generates a unique key for each day (day_key) and retrieves the event's unique identifier (event_id).
If the event ends at midnight (indicating it should not be counted for that day), it skips adding that event.
If the event is not already recorded for that day, it adds it to the day_events array.

$day_events = [];
foreach ($filtered as $timestamp => $events_in_day) {
foreach ($events_in_day as $event) {
if ($event instanceof Event) {
$day = intval(Carbon::createFromTimestamp($timestamp, $calendar->timezone)->endOfDay()->day);
$day_events[$day][] = $event;
$event_start = Carbon::createFromTimestamp($event->start, $calendar->timezone);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach ($day_events as $day_key => $events) {
This final loop ensures that the events for each day are stored as a simple indexed array, removing any gaps in the keys. This is useful for consistent access to the events later in the code.

Overall, this code snippet filters and organizes calendar events based on their start and end times, ensuring that events are correctly attributed to the days they occur. It handles edge cases, such as events that span multiple days and those that end exactly at the start of a new day, to prevent double counting.

$day_key = intval($day->format('j'));
$event_id = $event->uid;

if ($day->isSameDay($event_end) && $event_end->hour == 0 && $event_end->minute == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add comment which kind of case does it handle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simple $response variable fix
2 participants